/**
 * Created by xuweijie on 17-5-8.
 */
//注意：选项卡 依赖 element 模块，否则无法进行功能性操作
layui.use('element', function(){
    var element = layui.element();

    //…
});

//layer弹出层
layui.use('layer', function () {
    var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
    //触发事件
    var active={
        update: function (othis) {
            var content=$('#userupdatemodel');
            layer.open({
                type: 1,
                title:'修改用户信息',
                skin: 'layui-layer-molv', //样式类名
                closeBtn: false, //不显示关闭按钮
                anim: 2,
                area: '800px;',
                btn: ['绝不后悔', '我怂了'],
                shadeClose: true, //开启遮罩关闭
                content: content,
                success: function (layero) {
                    var userInfoParent = $(othis).parents('tr');
                    var userInfoItem = userInfoParent.children("td");
                    $('#upusername').val(userInfoItem[1].innerText);
                    $('#upphone').val(userInfoItem[5].innerText);

                    //角色
                    var radioRole = $("#radio_role").children('div');
                    $(radioRole).each(function () {
                        if (othis.rolename === $(this).children('span')[0].innerText){
                            $(this).addClass('layui-form-radioed');
                            $(this).prev('input').attr('checked',true);
                            var child_i = $(this).children('i');
                            child_i.addClass('layui-anim-scaleSpring');
                            child_i.html('');
                        }else {
                            $(this).removeClass('layui-form-radioed');
                            var child_i = $(this).children('i');
                            child_i.removeClass('layui-anim-scaleSpring');
                            child_i.html('');
                        }
                    });

                    //性别
                    var radioSex = $("#radio_sex").children('div');
                    $(radioSex).each(function () {
                        if (othis.sex === $(this).children('span')[0].innerText){
                            $(this).addClass('layui-form-radioed');
                            $(this).prev('input').attr('checked',true);
                            var child_i = $(this).children('i');
                            child_i.addClass('layui-anim-scaleSpring');
                            child_i.html('');
                        }else {
                            $(this).removeClass('layui-form-radioed');
                            var child_i = $(this).children('i');
                            child_i.removeClass('layui-anim-scaleSpring');
                            child_i.html('');
                        }
                    });

                },
                yes: function (index, layero) {

                    //性别
                    var rasex,raroleid;
                    $("input:radio[name=upsex]:checked").each(function(){
                        rasex=$(this).val();
                    });
                    $("input:radio[name=uproleid]:checked").each(function(){
                        raroleid=$(this).val();
                    });

                    var params = $.param({
                        username: $('#upusername').val(),
                        password: $('#uppassword').val(),
                        phone: $('#upphone').val(),
                        roleid:raroleid,
                        sex:rasex,
                        userid:othis.userid
                    },true);
                    $.ajax({
                        url: '/updateuser.action',
                        type: 'post',
                        data: params,
                        dataType: 'json',
                        success: function (data) {
                            if(data=="0"){
                                layer.msg("新密码不能与旧密码相同");
                            } else if(data=="2"){
                                layer.msg("修改成功");
                                window.location.href = "/admin/user/detail";//重新加载该页面
                            }else if(data=="1"){
                                layer.msg("修改成功,需要重新登录");
                                layer.close(index);
                                window.location.href = "/logout";
                                // var parient=$("#mauser" , parent.document);
                                // console.log(parient);
                            }else {
                                layer.msg("修改失败");
                            }
                        }
                    })
                }
            });
        },
        notice: function (othis) {
            //删除
            layer.open({
                type: 1,
                title: false, //不显示标题栏
                closeBtn: false,
                area: '300px;',
                shade: 0.8,
                id: 'LAY_layuipro', //设定一个id，防止重复弹出
                btn: ['绝不后悔', '我怂了'],
                moveType: 1, //拖拽模式，0或者1,
                content: '<div style="padding: 30px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">删除后将不可恢复，是否确认删除<br></div>',
                yes: function (index, layero) {
                    $.ajax({
                        url: "/deluser.action",
                        type: 'post',
                        data: {
                            userid: othis.userid
                        },
                        dataType: 'json',
                        success: function (data) {
                            if (data == "删除成功！") {
                                window.location.href = "/admin/user/detail";
                            }
                            layer.msg(data);
                        }
                    });
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                }
            });
        }
    };
    $('.updateuser').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.userid = $(this).attr('userid');
        othis.rolename=$(this).attr('rolename');
        othis.sex=$(this).attr('sex');
        active[method] ? active[method].call(this, othis) : '';
    });
    $('.deluser').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.userid = $(this).attr('userid');
        active[method] ? active[method].call(this, othis) : '';
    });
});

// 添加用户
// 表单显示
layui.use('form', function () {
    var form = layui.form();
    //监听提交
    form.on('submit(fromsubmit)', function (data) {
        console.log(data.field);
        var params = $.param({
            username: data.field.username,
            password: data.field.password,
            phone: data.field.phone,
            roleid: data.field.roleid,
            sex:data.field.sex
        },true);
        $.ajax({
            url: '/adduser.action',
            type: 'post',
            data: params,
            dataType: 'json',
            success: function (data) {
                console.log(data);
                window.location.href = "/admin/user/detail";
            }
        });

        return false;
    });
});

